#include "common.h"

int connect_to_server(){
	char* server = (char*)calloc(50, sizeof(char));
	int port;
	char *user = (char*)calloc(50, sizeof(char));
	char* password = (char*)calloc(50, sizeof(char));
	read_config(server, &port, user, password);
	
	int socket;
	struct sockaddr_in serv_addr;
    	struct hostent *srv;

	srv = gethostbyname(server);
    	if (srv == NULL) {
        	fprintf(stderr,"ERROR, no such host\n");
        	exit(0);
    	}
        memset((char *) &serv_addr, 0, sizeof(serv_addr));
    	serv_addr.sin_family = AF_INET;
    	
	memcpy((char *)&serv_addr.sin_addr.s_addr,
    	       (char *)srv->h_addr, 
               srv->h_length);
	
    	serv_addr.sin_port = htons(port);
    	
    	if (connect(socket,(struct sockaddr*) &serv_addr,sizeof(serv_addr)) < 0) 
    	    error("ERROR connecting");
    	return socket;
}
